package com.inxedu.os.edu.service.course.impl;


import com.inxedu.os.edu.dao.CourseKpointMapper;
import com.inxedu.os.edu.dao.EduCourseKpointMapper;
import com.inxedu.os.edu.pojo.CourseKpoint;
import com.inxedu.os.edu.pojo.CourseKpointDto;
import com.inxedu.os.edu.pojo.EduCourseKpoint;
import com.inxedu.os.edu.pojo.EduCourseKpointExample;
import com.inxedu.os.edu.service.course.CourseKpointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseKpointServiceImpl implements CourseKpointService {

    @Autowired
    CourseKpointMapper courseKpointMapper;

    @Autowired
    private EduCourseKpointMapper eduCourseKpointMapper;

    @Override
    public List<CourseKpoint> selectCourseKpoint(Integer courseId) {
        return courseKpointMapper.selectCourseKpointByCourseId(courseId);
    }

    @Override
    public int addCourseKpoint(CourseKpoint courseKpoint) {
        return courseKpointMapper.createCourseKpoint(courseKpoint);
    }

    @Override
    public CourseKpointDto queryCourseKpointById(int kpointId) {
        return courseKpointMapper.queryCourseKpointById(kpointId);
    }

    /**
     * 删除视频节点
     * @param ids ID串
     */
    @Override
    public void deleteKpointByIds(String ids){

        if (ids.endsWith(",")){
            ids = ids.substring(0, ids.length()-1);
        }
        courseKpointMapper.deleteKpointByIds(ids);

    }

    @Override
    public void updateKpointParentId(int kpointId, int parentId) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("kpointId", kpointId);
        map.put("parentId", parentId);
        courseKpointMapper.updateKpointParentId(map);
    }

    @Override
    public void updateKpoint(CourseKpoint kpoint) {
        courseKpointMapper.updateKpoint(kpoint);
    }

    @Override
    public EduCourseKpoint selectKpionByCourseIdAndKpointId(Integer courseId, Integer kpointId) {
        EduCourseKpointExample example = new EduCourseKpointExample();
        EduCourseKpointExample.Criteria criteria = example.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        criteria.andKpointIdEqualTo(kpointId);
        List<EduCourseKpoint> eduCourseKpoints = eduCourseKpointMapper.selectByExample(example);
        if (eduCourseKpoints!=null&&eduCourseKpoints.size()!=0){
            return eduCourseKpoints.get(0);
        }
        return null;
    }


}
